import { defineStore } from 'pinia'
import Center from '@/layout/center/center.vue'
// 获取菜单接口
import { getMenuListApi } from '@/api/menu/menu'
import { RouteRecordRaw } from 'vue-router'
import Layout from '@/layout/Index.vue'
const modules = import.meta.glob('../view/**/*.vue')
export interface MenuState {
  menuList: any
}
// 定义store
export const menuStore = defineStore('menuStore', {
  state: (): MenuState => {
    return {
      menuList: [
        {
          path: '/',
          component: "Layout",
          name: 'dashboard',
          redirect: '/dashboard',
          meta: {
            title: "首页",
            icon: "HomeFilled"
          },
          children: [
            {
              path: '/dashboard',
              component: () => import('@/view/dashboard/Index.vue'),
              name: 'dashboardIndex',
              meta: {
                title: '首页',
                icon: 'HomeFilled'
              }
            }
          ]
        },
        {
          path: '/organization',
          component: Layout,
          name: 'organization',
          meta: {
            title: "系统人员管理",
            icon: "HomeFilled"
          },
          children: [
            {
              path: '/03Organization/01User/index',
              component: () => import('@/view/03Organization/01User/index.vue'),
              name: '01User',
              meta: {
                title: '用户管理',
                icon: 'HomeFilled'
              }
            },
            {
              path: '/03Organization/02Department/index',
              component: () => import('@/view/03Organization/02Department/index.vue'),
              name: '02Department',
              meta: {
                title: '部门管理',
                icon: 'HomeFilled'
              }
            },
            {
              path: '/03Organization/03Role/index/index',
              component: () => import('@/view/03Organization/03Role/index.vue'),
              name: '03Role',
              meta: {
                title: '角色管理',
                icon: 'HomeFilled'
              }
            },
            {
              path: '/03Organization/04Menu/index',
              component: () => import('@/view/03Organization/04Menu/index.vue'),
              name: '04Menu',
              meta: {
                title: '菜单管理',
                icon: 'HomeFilled'
              }
            },
          ]
        },
      ]
    }
  },
  actions: {
    getMenuList(router: any) {
      return new Promise((resolve, reject) => {
        getMenuListApi().then(res => {
          let accessedRoutes;
          if (res.code == 200) {
            console.log('===== router');

            //动态生成路由
            accessedRoutes = filterAsyncRoutes(res.data, router);
            //设置到menuList
            this.menuList = this.menuList.concat(accessedRoutes)
          }
          //返回
          resolve(accessedRoutes);
        }).catch(error => {
          reject(error)
        })
      })
    }
  },
  getters: {

  }
})
export function filterAsyncRoutes(routes: RouteRecordRaw[], router: any) {
  const res: Array<RouteRecordRaw> = []
  routes.forEach((route: any, index: number) => {
    const tmp = { ...route }
    const newRouter: any = {}
    // name meta{title icon} path component redirect
    // 对component 怎么处理 parentId-component->Layout
    if (tmp.nodeType === 1 && tmp.parentId == 0 && tmp.code === 'dashboard') {
      newRouter.component = Layout
      newRouter.path = tmp.router
      newRouter.redirect = '/dashboard'
      newRouter.name = tmp.code
      newRouter.meta = {
        title: tmp.name,
        icon: tmp.nodeIcon
      }

    } else if (tmp.nodeType === 1 && tmp.parentId == 0 && tmp.code !== 'dashboard') {
      newRouter.component = Layout
      newRouter.path = tmp.router
      newRouter.name = tmp.code
      newRouter.meta = {
        title: tmp.name,
        icon: tmp.nodeIcon
      }
    } else if (tmp.nodeType === 2) {
      newRouter.component = modules[`../view${tmp.router}.vue`]
      newRouter.path = tmp.router
      newRouter.name = tmp.code
      newRouter.meta = {
        title: tmp.name,
        icon: tmp.nodeIcon
      }

    } else if (tmp.nodeType === 1 && tmp.parentId != 0) {
      newRouter.component = Center
      newRouter.path = tmp.router
      newRouter.name = tmp.code
      newRouter.meta = {
        title: tmp.name,
        icon: tmp.nodeIcon
      }
    }

    if (tmp.children && tmp.children.length > 0) {
      //如果有下级，并且component != 'Layout'
      if (route.nodeType === 1 && route.parentId != 0) {
        newRouter.component = Center
      }
      //递归
      newRouter.children = tmp.children
      newRouter.children = filterAsyncRoutes(tmp.children, router)
      console.log(newRouter, 'newRouter',tmp.children,'tmp.children');

      router.addRoute(newRouter)
      res.push(newRouter)
    }
  })
  return res
}